package com.baturu.vin.byd.dal.dao;

import com.baturu.vin.byd.dto.BydVinInfoDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface BydVinInfoDAO {

    @Select({"select vinCode,carSystem,year,carType,engine,colorName,gearBox,brandName,btrId,carSystemId,carTypeId,mjId from byd_vin",
            "where vinCode = #{vin} limit 1"})
    BydVinInfoDTO doFindVinInfo(@Param("vin") String vin);

    @Select({"select distinct id from byd_cartype_mj ",
            "where carsystemid = #{vinInfo.carSystemId}",
            "and cartypeid = #{vinInfo.carTypeId}",
            "and mjId = #{vinInfo.mjId} limit 1"})
    Integer getCarTypeMjId(@Param("vinInfo") BydVinInfoDTO vinInfoDTO);

    @Select({"<script>select distinct btrId from byd_cartype_btr",
            "where year=#{year}",
            "<if test=\"carType !=null and carType !=''\">",
            "and carType=#{carType}",
            "</if>",
            "<if test=\"carSystem !=null and carSystem !=''\">",
            "and carSystem=#{carSystem}",
            "</if>",
            "limit 1",
            "</script>"})
    String getBtrId(@Param("carSystem") String carSystem, @Param("carType") String carType, @Param("year") Integer year);

    @Select({"SELECT a.id as carSystemId, b.id as carTypeId, c.id as mjId FROM byd_carsystem as a, byd_cartype as b, byd_mj as c",
            "where a.carSystem = #{vinInfo.carSystem} and b.carType=#{vinInfo.carType} and c.mj = #{vinInfo.year} limit 1"})
    BydVinInfoDTO getCarSystemCarTypeMjId(@Param("vinInfo") BydVinInfoDTO vinInfoDTO);

    @Insert({"insert ignore into byd_cartype_btr(carSystem, carType, year, btrId) values",
            "(#{carSystem}, #{carType}, #{year}, #{btrId})"})
    int createCarTypeBtr(@Param("carSystem") String carSystem, @Param("carType") String carType, @Param("year") int year, @Param("btrId") String btrId);

}
